package com.teamin.wxqun;

import com.alibaba.fastjson.JSONObject;
import com.teamin.utils.WXAESUtils;
import com.teamin.utils.WxPKCS7Encoder;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;

public  class T {


        private Logger logger = LoggerFactory.getLogger(this.getClass());
        /**
         * 获取微信用户的基本信息
         *
         */
        @Test
        public void getUserInfo() throws IOException {
                logger.info(getSmallWX("011WxV2l0131qn1wiJ3l0YSZ2l0WxV2h","wx3455211af04c94db","bdd7299f7195428fb0ce11b053c23475"));
        }

        /**
         * 获取微信解密信息
         *
         */
        @Test
        public void pare() throws IOException {
                System.out.println( decodeEncrData("htIMr2gJ9aQVa/PvC6L2RUpqD1Hql4+Ey3LNujPUw1YaTrrdhTR5lnIVjiJJ9VUqpWFG/NyAyknncJoD6ymgCq9TbG4xFabqxYxAg/9m8Dxup3mZ5+JSQVFE+izenRQGGPbmj9v+vvqkz5lXWRGoVQaf51Je56ik+FZkjeSOVF2Mp3ciLudCgc9uETjScb64T1R9Kn+30f4UgAFwrQJseOysVgyIwS9Ro9/juvoYyr0msN0khw7nUBri7sKcwVorT25LPQjtiTAo+weDSAIHV2yH4xwl9lYdvOY095QjwZ3pKaBXF3FikIWv66+pA9QgLO+JS956zefl5WRsnMjs5JK2WpWvGnUA3ot2kl7HfReChzmyXoV9q+Msu/tNl1zCrzP8qGZKospw3kJIeWxpkCa5NBxRLLeiyNkbjuTocGU0uTBOibz04ZaB1mk3JcWH38C/r+UueLCCLtdly43/8rDZVJSClslYe4eSN9HqgLfAuFDMRwrtJdgZDh6X3umj",
                		"bUr3GBEADLDPpwmXvLcMkw==",
                		"4HlhI3p7CSYKERcbemXsHw=="));
        }

        /**
         * 解密微信数据
         * @param encrData
         * @param v1
         * @param sessionKey
         * @return
         */
        public JSONObject decodeEncrData(String encrData, String v1, String sessionKey) {

        try {
                WXAESUtils aes = new WXAESUtils();
                byte[] resultByte = aes.decrypt(Base64.getDecoder().decode(encrData), Base64.getDecoder().decode(sessionKey), Base64.getDecoder().decode(v1));
                if (null != resultByte && resultByte.length > 0) {
                        return JSONObject.parseObject(new String(WxPKCS7Encoder.decode(resultByte)));
                }
        } catch (Exception e) {
                e.printStackTrace();
        }
        return null;
        }

        /**
         * 获取用户信息
         * @param jsCode
         * @param appid
         * @param secret
         * @return
         * @throws IOException
         */
        public String getSmallWX(String jsCode, String appid, String secret) throws IOException {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code=" + jsCode + "&grant_type=authorization_code")
                        .build();
                Call call = okHttpClient.newCall(request);
                Response response = call.execute();
                return JSONObject.toJSONString(response.body().string());
        }
        
        @Test
		public void testBean() throws Exception {
        	Date currentDate = new Date();
    		Calendar cal = DateUtils.toCalendar(DateUtils.addDays(new Date(), 3));
    		cal.set(Calendar.HOUR_OF_DAY, 0);
    		cal.set(Calendar.MINUTE, 0);
    		cal.set(Calendar.SECOND, 0);
    		cal.set(Calendar.MILLISECOND, 0);
    		Date threeDate = cal.getTime();
    		Date sevenDate = DateUtils.addDays(threeDate, 4);
    		
    		System.out.println(DateFormatUtils.format(currentDate, "yyyy-MM-dd HH:mm:ss.SSS"));
    		System.out.println(DateFormatUtils.format(threeDate, "yyyy-MM-dd HH:mm:ss.SSS"));
    		System.out.println(DateFormatUtils.format(sevenDate, "yyyy-MM-dd HH:mm:ss.SSS"));
    		
		}
        
        @Test
    	public void testTime() throws Exception {
    		Long l = 0L;
    		Date date = new Date(l);
    		System.out.println(DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
    		
    		System.out.println(DateUtils.parseDate("1960-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss").getTime());
    	}
    	
        @Test
		public void testJson() throws Exception {
            logger.info(""+System.currentTimeMillis());
		}
}